package com.aier.proxy.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin.entity.IcCard;
import com.aier.common.dao.ICommonDAO;
import com.aier.proxy.entity.ProxyCity;
import com.aier.proxy.service.IProxyService;
import com.aier.proxy.service.IUserAssignCardService;
import com.aier.proxy.service.IUserCreateSchoolService;
import com.aier.school.entity.School;

@Scope("prototype")
@Component("proxyAssignIcCradsAction")
public class ProxyAssignIcCradsAction extends ProxyBaseAction {
    /**
     * 分派卡
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "proxyService")
    private IProxyService proxyService;
    @Resource(name = "userAssignCardService")
    private IUserAssignCardService userAssignCardService;
    @Resource(name = "userCreateSchoolService")
    private IUserCreateSchoolService userCreateSchoolService;

    // 页数
    private int page;
    // 每页显示记录数
    private int page_size;
    // 总页数
    private int page_count;
    // 总数
    private int count;
    // 错误码
    private int s;
    // 排序方式
    private String sord;
    // 排序字段
    private String sort;

    // 代理商ID
    private int pid;
    // 代理商拥有的卡
    private List<IcCard> icCards;

    private List<ProxyCity> proCityList;
    private List<School> schoolList;
    private List<IcCard> head_searchList;
    private String head_number;

    private IcCard ic;
    // 分派
    private Integer area_id;
    private Integer school_id;
    private String ic_head_add;
    private String body_begin;
    private String body_end;
    // 查询
    private Integer ic_state;
    private String ic_head;

    @Override
    public String execute() {
        head_searchList = proxyService.searchProCards(pid);
        if (head_searchList != null && head_searchList.size() > 0){
            head_number = "" + proxyService.searchProHeadNum(pid,head_searchList.get(0).getIchead());
        }else{
            head_number = "0";
        }
        proCityList = proxyService.searchProxyCity(pid);
        if (proCityList != null && proCityList.size() > 0) {
            schoolList = userCreateSchoolService.searchSchool(pid, proCityList
                    .get(0).getCid());
        }
        return SUCCESS;
    }

    public String changearea() {
        schoolList = userCreateSchoolService.searchSchool(pid, area_id);
        return SUCCESS;

    }

    public String changehead() {
        head_number = "" + proxyService.searchProHeadNum(pid,ic_head_add);
        return SUCCESS;
    }

    /**
     * 查询代理商拥有的IC卡
     * 
     * @param assignCardService
     */
    @SuppressWarnings("unchecked")
    public String search() {
        if (page <= 0) {
            page = 1;
        }
        int sortable = -1;
        try {
            if ("cardnum".equals(sort)) {
                sortable = ("asc".equals(sord)) ? IProxyService.CARD_SORTABLE_NUMBER
                        : IProxyService.CARD_SORTABLE_NUMBER_DESC;
            }

            Map<String, Object> map = proxyService.searchProCardsByCondition(
                    page, IProxyService.PROXY_CARD_LIST_PAGESIZE, sortable,
                    pid, ic_state, ic_head);
            if (null == map) {
                s = 0;
                return ERROR;
            }
            icCards = (List<IcCard>) map.get(ICommonDAO.OBJ_LIST);
            if (null == icCards) {
                s = 0;
                return SUCCESS;
            }
            page = (Integer) map.get(ICommonDAO.PAGE);
            page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
        } catch (Exception e) {
            e.printStackTrace();
            s = 0;
            return SUCCESS;
        }

        return SUCCESS;

    }

    public String assignCard() {
        try {
            List<IcCard> icList = proxyService.searchProCardUse(pid, body_begin, body_end,ic_head_add);//已分派
//            List<IcCard> noicList = proxyService.searchNoProCardUse(pid, body_begin, body_end,ic_head_add);//不可用
            if(icList!=null&&icList.size()>0){
                ic = icList.get(0);
                s = 0;
                return SUCCESS;            
            }else{
                //查询不是代理商的卡 -----增加
                userAssignCardService.insertICardToSchool(pid,ic_head_add, body_begin,body_end, school_id);            
            }
        } catch (Exception e) {
            e.printStackTrace();
            s = 0;
            return SUCCESS;    
        }
        s = 1;
        return SUCCESS;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public int getCount() {
        return count;
    }

    public int getS() {
        return s;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public List<IcCard> getIcCards() {
        return icCards;
    }

    public void setProxyService(IProxyService proxyService) {
        this.proxyService = proxyService;
    }


    public void setArea_id(Integer area_id) {
        this.area_id = area_id;
    }

    public void setSchool_id(Integer school_id) {
        this.school_id = school_id;
    }

    public void setIc_head_add(String ic_head_add) {
        this.ic_head_add = ic_head_add;
    }

    public void setBody_begin(String body_begin) {
        this.body_begin = body_begin;
    }

    public void setBody_end(String body_end) {
        this.body_end = body_end;
    }

    public void setIc_state(Integer ic_state) {
        this.ic_state = ic_state;
    }

    public void setIc_head(String ic_head) {
        this.ic_head = ic_head;
    }

    public IcCard getIc() {
        return ic;
    }

    public String getHead_number() {
        return head_number;
    }

    public List<ProxyCity> getProCityList() {
        return proCityList;
    }

    public List<School> getSchoolList() {
        return schoolList;
    }

    public void setUserAssignCardService(
            IUserAssignCardService userAssignCardService) {
        this.userAssignCardService = userAssignCardService;
    }

    public void setUserCreateSchoolService(
            IUserCreateSchoolService userCreateSchoolService) {
        this.userCreateSchoolService = userCreateSchoolService;
    }

    public List<IcCard> getHead_searchList() {
        return head_searchList;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    
}